﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Modules_Resource_Default : VST.Controls.BaseLCGPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        GridView1.DataSource = LocaleStringResourceManager.CreateInstant().SelectAllRDT();
        GridView1.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DataTable dt = LocaleStringResourceManager.CreateInstant().SelectAllRDT();
        // Here we'll add a blank row to the returned DataTable
        var dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        dt.AcceptChanges();
        //Creating the first row of GridView to be Editable
        GridView1.EditIndex = 0;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        //Changing the Text for Inserting a New Record
        ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Thêm";
        //((TextBox)GridView1.Rows[0].Cells[1].Controls[1]).Enabled = true;
        //((DropDownList)GridView1.Rows[0].Cells[2].Controls[1]).Enabled = true;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var r = (DataRowView)e.Row.DataItem;
            var ddlLanguageId = (DropDownList)e.Row.FindControl("ddlLanguageId");
            if (ddlLanguageId != null)
            {
                ddlLanguageId.SelectedValue = r["LanguageID"].ToString();
            }
            // loop all data rows
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    // Must use LinkButton here instead of ImageButton
                    // if you are having Links (not images) as the command button.
                    LinkButton button = control as LinkButton;
                    if (button != null && button.CommandName == "Delete")
                        // Add delete confirmation
                        button.OnClientClick = "if (!confirm('Are you sure " +
                               "you want to delete this record?')) return false;";
                }
            }
        }

    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        var ddlLanguageId = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlLanguageId");
        var txtResourceName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtResourceName");
        var txtResourceValue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtResourceValue");
        LocaleStringResourceManager.CreateInstant().InsertUpdate(new LocaleStringResourceEntity
        {
            LanguageID = ddlLanguageId.SelectedValue.ToInt(),
            ResourceName = txtResourceName.Text,
            ResourceValue = txtResourceValue.Text
        });

        GridView1.EditIndex = -1;
        BindData();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var hdfLanguagId = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfLanguagId");
        var hdfResourceName = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfResourceName");
        LocaleStringResourceManager.CreateInstant().Delete(hdfLanguagId.Value.ToInt(), hdfResourceName.Value);
        Response.Redirect(Request.RawUrl);
    }
}